package org.jeecg.modules.gongqiang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.sf.saxon.expr.Component;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.gongqiang.entity.*;
import org.jeecg.modules.gongqiang.mapper.*;
import org.jeecg.modules.gongqiang.service.IGoodService;
import org.jeecg.modules.gongqiang.service.IMachineSerialNumberService;
import org.jeecg.modules.gongqiang.service.INumberPlatformService;
import org.jeecg.modules.gongqiang.vo.NumberPlatformParams;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysAnnouncementSendMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * @Description: 机器序列号
 * @Author: jeecg-boot
 * @Date:   2024-06-12
 * @Version: V1.0
 */
@Service
public class MachineSerialNumberServiceImpl extends ServiceImpl<MachineSerialNumberMapper, MachineSerialNumber> implements IMachineSerialNumberService {


    @Autowired
    private INumberPlatformService numberPlatformService;

    @Resource
    private SerialNumberMapper serialNumberMapper;

    @Resource
    private MallConfigurationMapper mallConfigurationMapper;

    @Autowired
    private IGoodService goodService;


    @Resource
    private UserhomeOrderPlatformMapper userhomeOrderPlatformMapper;

    @Resource
    private OrderIndexArmMapper orderIndexArmMapper;


    @Override
   public Result<String> bingSerialNumber(MachineSerialNumber machineSerialNumber)
   {
       //查询是否有这个序列号 没用过期
       LambdaQueryWrapper<SerialNumber> querySerialNumber = new LambdaQueryWrapper<SerialNumber>();
       querySerialNumber.eq(SerialNumber::getNumber, machineSerialNumber.getSerialNumber());
       querySerialNumber.eq(SerialNumber::getGoodCode, machineSerialNumber.getGoodCode());
       querySerialNumber.eq(SerialNumber::getStatus, 1);
       SerialNumber  serialNumber=serialNumberMapper.selectOne(querySerialNumber);
       if(oConvertUtils.isEmpty(serialNumber))
       {
           return Result.error("序列号异常！");
       }
       LambdaQueryWrapper<MachineSerialNumber> machineSerialNumberOne = new LambdaQueryWrapper<MachineSerialNumber>();
       machineSerialNumberOne.eq(MachineSerialNumber::getSerialNumber,machineSerialNumber.getSerialNumber());
       machineSerialNumberOne.eq(MachineSerialNumber::getGoodCode,machineSerialNumber.getGoodCode());
       long count=this.baseMapper.selectCount(machineSerialNumberOne);
       machineSerialNumberOne.eq(MachineSerialNumber::getMachineNum,machineSerialNumber.getMachineNum());
       MachineSerialNumber  machineSerialNumberTemp=this.baseMapper.selectOne(machineSerialNumberOne);
       if(oConvertUtils.isNotEmpty(machineSerialNumberTemp))
       {
           return Result.ok("成功！");
       }
       LambdaQueryWrapper<MallConfiguration> queryMallConfiguration= new LambdaQueryWrapper<>();
       queryMallConfiguration.eq(MallConfiguration::getMkey,"max_machine");
       int maxNum= Integer.parseInt(mallConfigurationMapper.selectOne(queryMallConfiguration).getMvalue());
       //如果超过限制
       if(count>=maxNum)
       {
           return Result.error("序列号绑定机器过多！");
       }

       if(count==0)
       {
           if(oConvertUtils.isEmpty(serialNumber.getBeginTime()))
           {
               //如果是新的序列号计算开始结束时间
               serialNumber.setBeginTime(new Date());
               LocalDateTime today = LocalDateTime.now();
               LocalDateTime inFiveDays = today.plusDays(serialNumber.getTermId());
               Date date = Date.from(inFiveDays.atZone(ZoneId.systemDefault()).toInstant());
               serialNumber.setEndTime(date);
           }
       }
       serialNumber.setMachineNum(serialNumber.getMachineNum()+1);
       serialNumberMapper.updateById(serialNumber);
       machineSerialNumber.setGoodCode(serialNumber.getGoodCode());
       machineSerialNumber.setGoodId(serialNumber.getGoodId());
       machineSerialNumber.setOrderNum(serialNumber.getOrderNum());
       machineSerialNumber.setUserId(serialNumber.getUserId());
       this.baseMapper.insert(machineSerialNumber);
       return Result.ok("成功！");
   }

    public Result<MachineSerialNumber> checkSerialNumber(MachineSerialNumber machineSerialNumber)
    {
        LambdaQueryWrapper<MachineSerialNumber> machineSerialNumberOne = new LambdaQueryWrapper<MachineSerialNumber>();
        machineSerialNumberOne.eq(MachineSerialNumber::getSerialNumber,machineSerialNumber.getSerialNumber());
        machineSerialNumberOne.eq(MachineSerialNumber::getGoodCode,machineSerialNumber.getGoodCode());
        machineSerialNumberOne.eq(MachineSerialNumber::getMachineNum,machineSerialNumber.getMachineNum());
        MachineSerialNumber  machineSerialNumberTemp=this.baseMapper.selectOne(machineSerialNumberOne);
        if(oConvertUtils.isNotEmpty(machineSerialNumberTemp))
        {
            //查询这个序列号的过期时间
            LambdaQueryWrapper<SerialNumber> querySerialNumber = new LambdaQueryWrapper<SerialNumber>();
            querySerialNumber.eq(SerialNumber::getNumber, machineSerialNumber.getSerialNumber());
            querySerialNumber.eq(SerialNumber::getGoodCode, machineSerialNumber.getGoodCode());
            SerialNumber  serialNumber=serialNumberMapper.selectOne(querySerialNumber);
            machineSerialNumberTemp.setEndTime(serialNumber.getEndTime());
            machineSerialNumberTemp.setStatus(serialNumber.getStatus());
            if(machineSerialNumberTemp.getStatus()==0)
            {
                return Result.error("序列号禁止！",machineSerialNumberTemp);
            }
            else if(machineSerialNumberTemp.getStatus()==2)
            {
                return Result.error("序列号已过期！",machineSerialNumberTemp);
            }
            else if(machineSerialNumberTemp.getStatus()==1)
            {
                //查询订单相关配置
                LambdaQueryWrapper<UserhomeOrderPlatform> orderQueryWrapper=new LambdaQueryWrapper<>();
                orderQueryWrapper.eq(UserhomeOrderPlatform::getSerialNumberId,serialNumber.getId());
                UserhomeOrderPlatform userhomeOrderPlatform=userhomeOrderPlatformMapper.selectOne(orderQueryWrapper);
                LambdaQueryWrapper<OrderIndexArm> orderIndexArmQueryWrapper=new LambdaQueryWrapper<>();
                orderIndexArmQueryWrapper.eq(OrderIndexArm::getSerialNumberId,serialNumber.getId());
                OrderIndexArm orderIndexArm=orderIndexArmMapper.selectOne(orderIndexArmQueryWrapper);
                machineSerialNumberTemp.setUserhomeOrderPlatform(userhomeOrderPlatform);
                machineSerialNumberTemp.setOrderIndexArm(orderIndexArm);
                NumberPlatformParams numberPlatformParams=numberPlatformService.searhInfos(serialNumber.getId());
                machineSerialNumberTemp.setAllParams(numberPlatformParams);

                LambdaQueryWrapper<Good> goodQueryWrapper=new LambdaQueryWrapper<>();
                goodQueryWrapper.eq(Good::getGoodCode,serialNumber.getGoodCode());
                Good good=goodService.getOne(goodQueryWrapper);
                machineSerialNumberTemp.setAdditionalUrl("https://after.wflike.com/jeecg-boot/sys/common/static/"+good.getAdditionalUrl());
                return Result.ok(machineSerialNumberTemp);
            }
            else
            {
                return Result.error("序列号状态异常！",machineSerialNumberTemp);
            }

        }
        else
        {

            MachineSerialNumber machineSerialNumberTemp2=new MachineSerialNumber();
            machineSerialNumberTemp2.setStatus(-1);
            return Result.error("序列号不存在！",machineSerialNumberTemp2);
        }
    }
}
